﻿@model dynamic
<script>


    $(function () {
        function initTools(option) {
            var config = {
                positionInputDisplay: '@("Position Name".Localize())',
                positionNullAlert: '@("Please input a new position name.".Localize())',
                nameRepeat: '@("Please input another name .".Localize())',
                wrongPosition: '@("Please choose another position".Localize())',
                textAreaId: "#Body",
                EditText: '@("Edit".Localize())',
                EditTitle: '@("Edit DataRule".Localize())',
                removeConfirm: '@("Are you sure you want to remove this item?".Localize())',
                itemExist: '@("This item already exists.".Localize())'
            };

            $.extend(config, option);

            var textArea = $(config.textAreaId);
            var $form = $("div.common-form form");

            $form.find('input').keydown(function (e) {
                if (e.keyCode == 13) {
                    return false;
                }
            });

            (function initTextarea() {
                textArea.codeMirror();
            })();
            var codeMirrorAPI = textArea.data("codeMirror");
            codeMirrorAPI.on('change', function (e) {
                if (e && e.historySize().undo > 0) {
                    window.leaveConfirm.stop();
                } else {
                    window.leaveConfirm.pass();
                }
            });
            (function initViewTools() {
                var viewTools = $("div.viewTools");
                var hasSubLi = viewTools.find("li.has-sub");


                $(".common-form .panel .block").each(function (index) {
                    var current = $(this);
                    var content = current.find("div.container");

                    var title = current.find(".title");
                    title.click(function () {
                        title.toggleClass('active');
                        content.slideToggle("fast");
                    });
                    index++;
                });

                viewTools.find("a").click(function () {

                    var current = $(this);
                    var codeMirrorAPI = textArea.data("codeMirror");

                    var code = current.attr('title');
                    codeMirrorAPI.insertAtCursor(code);
                });

            })();

            window.ajaxFormParam = {
                beforeSerialize: function ($form, options) {
                    var codeMirrorAPI = textArea.data("codeMirror");
                    var text = codeMirrorAPI.getText();
                    textArea.val(text);
                }
            };
        };
        function init() {
            $("div.task-block").each(function (index) {
                var current = $(this);
                var content = current.find("div.content");

                var h3 = current.find("h3.title").css({ cursor: 'pointer' });
                h3.click(function () {
                    var span = h3.find('span');
                    if (span.hasClass('close')) {
                        span.removeClass("close");
                        content.slideDown("fast");
                    } else {
                        content.slideUp("fast");
                        span.addClass("close");
                    }
                });
                index++;
            });

            initTools();
        };
        init();
    });

    (function panelList() {
        $(".common-form .panel div.list").find(".has-sub > span").click(function () {
            $(this).parent().toggleClass('active');
            $(this).siblings('ul').slideToggle('fast');
        });
    })();
</script>
